package com.plm.mqshequ.controller;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import com.plm.mqshequ.utils.ResultMsg;
import com.plm.mqshequ.utils.StrUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Map;

/**
 * 验证码控制类
 * @author Limi
 * @create 2019-01-18 10:39
 */
@Controller
@RequestMapping("/authcode")
public class AuthcodeController {

    @Autowired
    private Producer producer;

    /**
     * 随机字符串验证码
     * @param request
     * @param response
     * @throws IOException
     */
    @GetMapping("/captcha.jpg")
    public void captcha(HttpServletRequest request, HttpServletResponse response)throws IOException {
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");

        //生成文字验证码
        String text = producer.createText();
        //生成图片验证码
        BufferedImage image = producer.createImage(text);
        //保存到 session
        request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, text);

        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image, "jpg", out);
    }

    /**
     * 简单算数验证码
     * @param request
     * @param response
     * @throws IOException
     */
    @GetMapping("/calculate")
    public void calculate(HttpServletRequest request, HttpServletResponse response)throws IOException {
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");
        //生成算数计算验证码
        Map<String, String> map = StrUtils.authcode();
        String text = map.get("authcode");
        String result = map.get("result");
        //生成图片验证码
        BufferedImage image = producer.createImage(text);
        //保存到 session
        Session session = SecurityUtils.getSubject().getSession();
        session.setAttribute(Constants.KAPTCHA_SESSION_KEY, result);

        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image, "jpg", out);
    }

    @PostMapping("/authImageCode")
    public ResultMsg authCaptcha(@RequestParam String imagecode){
        Session session = SecurityUtils.getSubject().getSession();
        String text = (String) session.getAttribute(Constants.KAPTCHA_SESSION_KEY);
        if (!text.equals(imagecode.trim())) {
            return ResultMsg.error("验证码输入有误");
        }
        return ResultMsg.ok();
    }
}
